jQuery(function($) {
    Handlebars.registerHelper("ifnull", function(value) {
    	if (value == 0||value) {
			return value;
		}
        return " ";
    });

    $("#search").click(function() {
        var acctype = $("[name='acctype']").val();
        var accvalue = $("[name='accvalue']").val();
        var url = '';
        if (acctype == 'uid') {
            url = '/web/user/list.json?uid=' + accvalue;
        } else if (acctype == 'phone') {
            url = '/web/user/list.json?phone=' + accvalue;
        }else if(acctype=='tradeno'){
        	url = '/web/user/list.json?tradeno=' + accvalue;
        }else{
        	 url = '/web/user/list.json';
        }
        $.get(ctx+url).success(function(json) {
        	$(".tmp").remove();
        	if(json.c==0&&json.d){
        		var data = json.d;
        		for(var i=0;i<data.length;i++){
        			if(data[i].orders){
        				for(var j;j<data[i].orders.length;j++){
        					var order = data[i].orders[j];
        					if(order.payType=='wx'&&order.status=='PAYED'){
        						order.canRefund=true;
        					}
        				}
        			}
        		}
        	}
        	var html = $("#user-tpl").html();
            var tpl = Handlebars.compile(html);
            $("#user-tpl").after(tpl(json.d));
            
            $(".am-btn-toolbar .am-icon-pencil-square-o").click(function() {
                $tr = $(this).closest("tr");
                var uid = $tr.find("td:eq(1)").html().trim();
                var phone = $tr.find("td:eq(2)").html().trim();
                var nick = $tr.find("td:eq(3)").html().trim();
                var lvl = $tr.find("td:eq(5)").html().trim();
                $("#userEditLayer .am-modal-hd").html("用户:[" + uid + "][" + phone + "][" + nick+"]");
                $("#userEditLayer .am-modal-bd .am-modal-prompt-input").val(lvl);
                $("#userEditLayer").modal({
                    relatedTarget: this,
                    onConfirm: function(e) {
                        $.getJSON(ctx+"/web/user/update.json?id=" + uid + "&lvl=" + e.data.trim(),function(json) {
                            if (json.c == 0) {
                            	$("#search").click();
                                alert("保存成功 !");
                            } else {
                                alert("保存失败,code:" + json.c + ",msg:" + json.m);
                            }
                            $("#search").click();
                        });
                    }
                });
            });
            $(".am-btn-toolbar .am-icon-trash-o").click(function() {
                $tr = $(this).closest("tr");
                var uid = $tr.find("td:eq(1)").html().trim();
                $.getJSON(ctx+"/web/user/delete.json?uid=" + uid,function(json) {
                    if (json.c == 0) {
                        alert("删除成功 !");
                    } else {
                        alert("删除失败,code:" + json.c);
                    }
                    $("#search").click();
                });
            });
            $(".am-icon-rmb").click(function() {
                $tr = $(this).closest("tr");
                var tradeno = $tr.find("td:eq(1)").html().trim();
                $.getJSON(ctx+"/web/user/wxrefund.json?tradeno=" + tradeno,function(json) {
                    if (json.c == 0) {
                        alert("退款成功 !");
                    } else {
                        alert("退款失败,code:" + json.c);
                    }
                    $("#search").click();
                });
            });
        });
    });
});